package cn.com.taiji.pos.common.util;


import com.alibaba.fastjson.JSONObject;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class HttpUtil {


    public static String sendToPost(String url) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = "";
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();//设置请求和传输超时时间
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(requestConfig);
        try {
            response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
            if (response != null) {
                httpClient.close();
                response.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }

    public static String sendToGet(String url) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = "";
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build();//设置请求和传输超时时间
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(requestConfig);
        try {
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
            if (response != null) {
                httpClient.close();
                response.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }

    public static String sendToPostWithParameter(String url, Map<String, String> map) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = "";
        List<NameValuePair> paramsList = new ArrayList<>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            //给参数赋值
            paramsList.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(paramsList, Consts.UTF_8);
        HttpPost httpPost = new HttpPost(url);
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();//设置请求和传输超时时间
        httpPost.setConfig(requestConfig);
        httpPost.setEntity(urlEncodedFormEntity);
        try {
            response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return result;
        } finally {
            if (response != null) {
                try {
                    httpClient.close();
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public static String sendToGetWithParameter(String url, Map<String, Object> map) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = "";
        String params = "?";
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            //参数拼接
            params += entry.getKey() + "=" + JSONObject.toJSONString(entry.getValue()) + "&";
        }
        url += params.substring(0, params.length() - 1);
        HttpGet httpGet = new HttpGet(url);
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(300).setConnectTimeout(300).build();//设置请求和传输超时时间
        httpGet.setConfig(requestConfig);
        try {
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
            if (response != null) {
                httpClient.close();
                response.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }

}
